package com.example.testapp.activity.util;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;

/**
 * Created by l on 2016/7/3.
 */
public class DisplayUtil {

    private static boolean mInitialed;
    public static int SCREEN_WIDTH_PIXELS;
    public static int SCREEN_HEIGHT_PIXELS;
    public static float SCREEN_DENSITY;
    public static int SCREEN_WIDTH_DP;
    public static int SCREEN_HEIGHT_DP;

    public static void init(Context context) {
        if (mInitialed || context == null){
            return;
        }
        mInitialed = true;
        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager  manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        manager.getDefaultDisplay().getMetrics(metrics);
        SCREEN_WIDTH_PIXELS = metrics.widthPixels;
        SCREEN_HEIGHT_PIXELS = metrics.heightPixels;
        SCREEN_DENSITY = metrics.density;
        SCREEN_WIDTH_DP = (int) (SCREEN_WIDTH_PIXELS / metrics.density);
        SCREEN_HEIGHT_DP = (int) (SCREEN_HEIGHT_PIXELS / metrics.density);
    }

    public static int sp2dp(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int)(pxValue / scale + 0.5f);
    }
}
